#!/usr/bin/python
# -*- coding : UTF-8 -*-
# ========================
# @Time  : 2020/5/8 9:25
# @Author: Mecell
# @File  : log_packaging.py
# ========================

import os
import logging

from concurrent_log_handler import ConcurrentRotatingFileHandler

from common.config_packaging import config_obj
from common.path_constants import LOG_DIR


class LogHandle:
    """
    定义一个日志处理的类
    """
    def __init__(self):
        self.logger = logging.getLogger(config_obj("log info", "logger_name"))
        self.logger.setLevel(config_obj("log info", "logger_level"))
        console_handler = logging.StreamHandler()
        file_handler = ConcurrentRotatingFileHandler(filename=os.path.join(LOG_DIR, config_obj("log info", "log_filename")),
                                                     maxBytes=config_obj("log info", "log_maxByte"),
                                                     backupCount=config_obj("log info", "log_bkCount"),
                                                     encoding="utf-8")
        console_handler.setLevel(config_obj("log info", "console_level"))
        file_handler.setLevel(config_obj("log info", "file_level"))
        simple_format = logging.Formatter(config_obj("log info", "simple_formatter"))
        verbose_format = logging.Formatter(config_obj("log info", "verbose_formatter"))
        console_handler.setFormatter(simple_format)
        file_handler.setFormatter(verbose_format)
        self.logger.addHandler(console_handler)
        self.logger.addHandler(file_handler)

    @property
    def get_logger(self):
        """
        返回日志收集器
        :return:
        """
        return self.logger


logger = LogHandle().get_logger


if __name__ == '__main__':
    import time
    for _ in range(5):
        time.sleep(0.5)
        logger.debug('这是debug级别的日志')
        logger.info('这是info级别的日志')
        logger.warning('这是warning级别的日志')
        logger.error('这是error级别的日志')
        logger.critical('这是critical级别的日志')